﻿package rd.android.hardcore.ui.pad
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import rd.android.hardcore.params.GlobalParams;
	
	/**
	 * pad front display
	 * @author Romain Prache
	 */
	public class FrontPad extends Sprite
	{
		/* VAR */
		private var maskT	: Sprite;
		private var loader	: Loader;
		private var bmp		: Bitmap;
		
		/* TODO
		 * BG !
		 */ 
		
		/* Front Pad */
		public function FrontPad() 
		{
			loader = new Loader();
			
			maskT = new Sprite();
			addChild(maskT);
			
			maskT.graphics.beginFill(0xFFFA00);
			maskT.graphics.drawRect(0, 0, GlobalParams.PAD_SIZE, GlobalParams.PAD_SIZE);
			maskT.graphics.endFill();
			
			buttonMode		= true;
						
		}
		
		
		public function resize(size:int):void
		{
			maskT.graphics.clear();
			
			maskT.graphics.beginFill(0xFFFA00);
			maskT.graphics.drawRect(0, 0, size, size);
			maskT.graphics.endFill();
		}
		
			
		/* Thumbnail Config */
		public function setThumb( _target : String ) : void
		{
			if ( _target == "" ) return; 
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayThumb);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, fail);
			
			loader.load( new URLRequest(_target));
		}
		
		private function displayThumb(e:Event):void 
		{
			
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, displayThumb);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, fail);
			
			bmp = loader.content as Bitmap;
			bmp.smoothing = true;
			
			var ratio : Number = bmp.height / GlobalParams.PAD_SIZE;
			
			bmp.height 	= GlobalParams.PAD_SIZE;
			bmp.width	= bmp.width / ratio; 
			
			bmp.x = (GlobalParams.PAD_SIZE - bmp.width) 	>> 1;
			bmp.y = (GlobalParams.PAD_SIZE - bmp.height) 	>> 1;
			
			addChild(bmp);
		}
		
		private function fail(e:IOErrorEvent):void 
		{
			trace("3: fail");
		}
	
	}
	
}